<template>
  <div class="home">
    <div class="home-search">
      <van-search
        v-model="value"
        shape="round"
        background="#aacfcf"
        placeholder="请输入搜索关键词"
      />
    </div>
    <div class="swiper-container">
      <div class="swiper-wrapper">
          <div
            class="swiper-slide"
            v-for="banner in banners"
            :key="`banner-${banner.id}`"
          >
            <img :src="banner.imageUrl" :title="banner.title">
          </div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
    <!-- 四个图文 -->
    <div class="home-text">
      <dl>
        <dt><img src="https://img.alicdn.com/tfs/TB1Cq1HekvoK1RjSZPfXXXPKFXa-502-380.jpg_240x240q90.jpg" alt=""></dt>
        <dd>9.9专区</dd>
      </dl>
      <dl>
        <dt><img src="https://aecpm.alicdn.com/simba/img/TB13xKuLVXXXXcHapXXSutbFXXX.jpg" alt=""></dt>
        <dd>超值大额劵</dd>
      </dl>
      <dl>
        <dt><img src="https://aecpm.alicdn.com/simba/img/TB1X6uHLVXXXXcCXVXXSutbFXXX.jpg" alt=""></dt>
        <dd>秋季上新</dd>
      </dl>
      <dl>
        <dt><img src="https://aecpm.alicdn.com/simba/img/TB1j4LLKpXXXXcbaXXXSutbFXXX.jpg" alt=""></dt>
        <dd>潮流在线</dd>
      </dl>
    </div>
    <div class="home-like">
      <!-- 无限滚动条模板 -->
      <van-list
        v-model="loading"
        :finished="isEnd"
        finished-text="没有更多了"
        @load="getData"
      >
        <van-cell
          v-for="shop in cateList"
          :key="`shop-${shop.id}`"
        >
          <template #title>
            <CateItem
              :id="shop.id"
              :title="shop.title"
              :image="shop.image"
              :price="shop.price"
              :saleNum="shop.saleNum"
              :type="shop.type"
            />
          </template>
        </van-cell>
      </van-list>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.css'
import CateItem from '../components/CateItem.vue'

export default {
  name: 'Home',
  data () {
    return {
      banners: [],
      id: '',
      cateList: [],
      // 请求数据起始索引
      start: 0,
      // 是否加载完成
      isEnd: false,
      // 是否正在加载，如果正在加载不允许发送重复的请求
      loading: false,
      value: ''
    }
  },
  components: {
    CateItem
  },
  created () {
    this.$http.getCategory(1)
      .then(resp => {
        // console.log(resp)
        this.banners = resp.banners
        this.$nextTick().then(() => {
          this.initBanner()
        })
      })
    this.id = this.$route.params.id
  },
  beforeRouteUpdate (to, from, next) {
    this.id = to.params.id
    next()
    // 先把start重置为0，cateList清空
    this.start = 0
    this.cateList = []
    this.getData()
  },
  methods: {
    initBanner () {
      // eslint-disable-next-line
      new Swiper ('.swiper-container', {
        direction: 'horizontal', // 垂直切换选项
        loop: true, // 循环模式选项
        pagination: {
          el: '.swiper-pagination'
        },
        autoplay: true
      })
    },
    getData () {
      this.$http.getHomeList(this.start)
        .then(resp => {
          // console.log(resp)
          const { list, nextIndex, isEnd } = resp
          this.cateList = this.cateList.concat(list)
          this.start = nextIndex
          this.isEnd = isEnd
          this.loading = false
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.home{
  height: 100%;
  background-color: #e8e8e8;
  overflow: auto;
  .swiper-container {
    width: 100%;
    height: 0;
    padding-top: 378 / 1008 * 100%;
    position: relative;
    .swiper-wrapper {
      position: absolute;
      left: 0;
      top: 0;
    }
    img {
      width: 100%;
    }
  }
  &-text{
    height: 90px;
    width: 95%;
    margin: 10px auto;
    border-radius: 10px;
    background-color: white;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    dd {
      font-size: 13px;
    }
    dl{
      height: 70px;
      width: 70px;
      text-align: center;
      display: flex;
      flex-direction: column;
      align-items: center;
      dt{
        height: 50px;
        width: 50px;
        border-radius: 50%;
        line-height: 50px;
        img{
          width: 100%;
          height: 100%;
          border-radius: 50%;
        }
      }
    }
  }
  &-like{
    .van-list {
      display: flex;
      flex-wrap: wrap;
    }
    .van-cell {
      width: 46%;
      margin: 2%;
      padding: 2%;
      font-size: 12px;
      line-height: 2;
      color: #666;
      background-color: #fff;
      border-radius: 1%;
    }
    .van-cell__title {
      width: 100%;
    }
  }
}

</style>
